home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
listings
/
v_12_03
/
kamradt
/
pcount.h
< prev
next >
Wrap
C/C++ Source or Header
|
1994-02-07
|
801b
|
48 lines
class String {
private:
struct StringImp {
char *ptr;
unsigned count;
StringImp(const char *str)
: ptr(strdup(str)),
count(1)
{
;
}
~StringImp()
{
delete[] ptr;
}
} *imp;
public:
String(const char *str)
{
imp = new StringImp(str);
}
String(const String &str)
{
imp = str.imp;
imp->count++;
}
// assignment is a little tricky
String &
operator=(const String &str)
{
// increment first in case
// of assignment to self
str.imp->count++;
// be sure to clean up the old imp!
if(--imp->count == 0)
delete imp;
imp = str.imp;
return *this;
}
~String()
{
if(--imp->count == 0)
delete imp;
}
};